//
//  pingViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/23.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "pingViewController.h"

static NSString *const Ocell = @"Ocell";

@interface pingViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *myTable;
@property(nonatomic, retain)UITextField *myText;
@property(nonatomic, retain)UIView *viewX;
@property(nonatomic, retain)NSMutableArray *myArray;

@end

@implementation pingViewController

-(void)dealloc{
    
    [_myView release];
    [_myTable release];
    [_model release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor yellowColor];
//    self.myArray = [NSMutableArray array];
    
//冒充导航
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
    self.myView.backgroundColor = [UIColor colorWithRed:171/255.0 green:186/255.0 blue:190/255.0 alpha:1];
    [self.view addSubview:self.myView];
   
    UILabel *labelT = [[UILabel alloc] initWithFrame:CGRectMake(self.myView.frame.size.width/2-20, self.myView.frame.size.height/2, 40, 20)];
    labelT.text = @"评论";
    labelT.backgroundColor = [UIColor clearColor];
    [self.myView addSubview:labelT];
    
//返回按钮
    FengButton *buttonF = [FengButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(5, 25, 30, 30) title:@"" image:[UIImage imageNamed:@"124"] color:[UIColor clearColor] handler:^(UIButton *sender) {
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
        
    }];
    [self.myView addSubview:buttonF];
    
    
    self.myTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height-100) style:UITableViewStylePlain];
    self.myTable.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTable];
    self.myTable.delegate = self;
    self.myTable.dataSource = self;
    self.myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.myTable registerClass:[pingLunCell class] forCellReuseIdentifier:Ocell];
    
    self.viewX = [[UIView alloc] initWithFrame:CGRectMake( 0, self.view.frame.size.height-40, self.view.frame.size.width, 40)];
    self.viewX.backgroundColor = [UIColor colorWithRed:179/255.0 green:198/255.0 blue:202/255.0 alpha:1];
    [self.view addSubview:self.viewX];
    
    UIImageView *imageY = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"123"]];
    imageY.backgroundColor = [UIColor clearColor];
    imageY.frame = CGRectMake(10, 10, 20, 20);
    [self.viewX addSubview:imageY];
    
    self.myText = [[UITextField alloc] initWithFrame:CGRectMake( 40, 10, self.view.frame.size.width-90, 20)];
    self.myText.backgroundColor = [UIColor whiteColor];
    [self.viewX addSubview:self.myText];
    self.myText.placeholder = @" 评论:说点什么吧....";
    self.myText.font = [UIFont systemFontOfSize:15];
    
    UIButton *buttonP = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonP.frame = CGRectMake(self.myText.frame.size.width+self.myText.frame.origin.x, self.myText.frame.origin.y, 50, 20);
    [buttonP setTitle:@"评论" forState:UIControlStateNormal];
    buttonP.font = [UIFont systemFontOfSize:15];
    [self.viewX addSubview:buttonP];
    [buttonP addTarget:self action:@selector(buttonPAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //监听键盘弹起
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    //监听键盘回收
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    
    [self.myText release];
    [self.myTable release];
    [self.myView release];
    
}

//上升
-(void)keyBoardWillShow:(NSNotification *)notif{
    //获取键盘的frame
    CGRect frame = [[[notif userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
    [UIView animateWithDuration:0.2 animations:^{
        self.viewX.frame = CGRectMake(0, self.view.frame.size.height-40 - frame.size.height, self.view.frame.size.width, 40);

    }];
}

//下降
-(void)keyBoardWillHide:(NSNotification *)notif{
    [UIView animateWithDuration:0.2 animations:^{
        
        self.viewX.frame = CGRectMake(0, self.view.frame.size.height-40, self.view.frame.size.width, 40);
    }];
    
}


//键盘回收
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.myText resignFirstResponder];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.myText resignFirstResponder];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    pingLunCell *cell = [tableView dequeueReusableCellWithIdentifier:Ocell forIndexPath:indexPath];
    
    commentlist *model = [self.myArray objectAtIndex:indexPath.row];
    cell.model = model;
    
    
    NSLog(@"%@",model.username);
    
    cell.selectionStyle = NO;
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.myArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    commentlist *model = [self.myArray objectAtIndex:indexPath.row];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName,nil];
    CGRect rect = [model.content boundingRectWithSize:CGSizeMake(self.view.frame.size.width-80, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rect.size.height+50;
}

#warning 评论按钮点击
-(void)buttonPAction:(UIButton *)button{
    
    NSLog(@"评论");
}

-(void)setModel:(expressBiao *)model{
    
    _model = model;

    self.myArray = [NSMutableArray arrayWithArray:_model.commentlist];
    
    NSLog(@"%@",_model);
    
    [self.myTable reloadData];
}


@end
